package cn.agiledata.bank.transaction.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.LongList;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.Bank;
import cn.agiledata.bank.hibernateBean.BankInfo;
import cn.agiledata.bank.hibernateBean.SubAccount;
import cn.agiledata.bank.hibernateBean.Transaction;
import cn.agiledata.bank.netReturn.logic.NetReturnBean;
import com.vnv.infrastructure.logic.IPBHibernate3BaseMgr;

public class TransactionDaoImpl extends IPBHibernate3BaseMgr implements
        TransactionDao {
    public Transaction getTransactionByTransId(String transId) {
        String hql = "from Transaction t where t.transId = ?";
        List resultList = getHibernateTemplate().find(hql, transId);
        return resultList.size() == 0 ? null : (Transaction) resultList.get(0);
    }

    public Transaction getTransactionByDebitOrderId(SubAccount debit,
            String orderId) {
        String hql = "from Transaction t where t.debit=? and t.orderId=?";
        List list = getHibernateTemplate().find(hql,
                new Object[] { debit, orderId });
        return list.size() == 0 ? null : (Transaction) list.get(0);
    }

    public LongList getTransactionList(String[] actionTypes, Bank bank,
            Date beginDate, Date endDate) {
        String hql = "from Transaction t where t.actionType.id in (:actionTypeList) "
                + "and t.bankSeq=:bankId and t.beginDate>=:beginDate and t.beginDate<=:endDate "
                + "order by t.beginDate";
        String[] paramNames = { "actionTypeList", "bankId", "beginDate",
                "endDate" };
        List actionTypeList = new ArrayList();
        for (int i = 0; i < actionTypes.length; i++) {
            Long actionTypeId = new Long(actionTypes[i]);
            actionTypeList.add(actionTypeId);
        }
        Object[] paramValues = { actionTypeList, bank.getId(), beginDate,
                endDate };
        List resultList = getHibernateTemplate().findByNamedParam(hql,
                paramNames, paramValues);
        LongList resultLongList = new LongList();
        resultLongList.addAll(resultList);
        return resultLongList;
    }
    
    /**
     * 验证退款申请数据是否合法(退款接口调用)
     * @param account
     * @param returnMoneyList
     * @return
     */
    public List getValReturnMoney(Account account, List returnMoneyList) {
    	StringBuffer hql = new StringBuffer("Select trans from Transaction trans, SubAccount sub where");
    	hql.append(" trans.credit.id=sub.id and trans.transStatus = :transStatus");
    	hql.append(" and trans.credit.account=:account");
    	hql.append(" and not exists(select 1 from EsPrePayTrans pTrans where pTrans.transStatus in (:pTransStatus) ");
    	hql.append(" and pTrans.relating = trans.id )");
    	hql.append(" and ((trans.bankSeq <> 21 and trans.bankSeq <> 11) or trans.bankSeq is null)");
    	
    	hql.append(" and (");
    	for (int i = 0; i < returnMoneyList.size(); i++) {
    		NetReturnBean bean = (NetReturnBean) returnMoneyList.get(i);
    		hql.append(" trans.orderId='");
    		hql.append(bean.getOrderId());
    		hql.append("' or trans.transId='");
    		hql.append(bean.getOrderId());
    		hql.append("'   or");
    	}
    	String shql = hql.substring(0, hql.length()-3);
    	shql +=")";
    	
    	List listPTransStatus = new ArrayList();
        listPTransStatus.add(Constant.ES_TRANS_STATUS_TOPUBMIT);	// 待提交
        listPTransStatus.add(Constant.ES_TRANS_STATUS_UNAUDIT);		// 待审核
        listPTransStatus.add(Constant.ES_TRANS_STATUS_FINISH);		// 完成
        listPTransStatus.add(Constant.ES_TRANS_STATUS_UNPAY);		// 待支付
        listPTransStatus.add(Constant.ES_TRANS_STATUS_REJECTED);		// 审核拒绝
        
        String[] paramNames = { "transStatus", "account", "pTransStatus"};
        Object[] paramValues = {Constant.TRANS_STATUS_TRANSACTIONSUCCESS, account, listPTransStatus};
        
    	List resultList = getHibernateTemplate().findByNamedParam(shql,
                paramNames, paramValues);
    	return resultList;
    }
    
    /**
     * 取得支持退款的银行ID
     * 当前所有银行都支持，所以取得所有银行
     * 
     * @return
     */
    private List getSupportBank() {
    	String strOql = "select bi from BankInfo bi where bi.bankCode <> ?";
    	
    	// 农行不支持退款
    	List listParam = new ArrayList();
    	listParam.add(Constant.BANK_CODE_ABC);
    	List listResult = getHibernateTemplate().find(strOql,
				listParam.toArray());
    	
    	List listReturn = null;
    	if(listResult != null && listResult.size() > 0) {
    		listReturn = new ArrayList();
    		for (Iterator iter = listResult.iterator(); iter.hasNext();) {
				BankInfo bi = (BankInfo) iter.next();
				listReturn.add(bi.getId());
			}
    	}
    	
    	return listReturn;
    }
}
